package com.photoselector.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.photoselector.R;
import com.photoselector.listener.PhotoItemClickListener;
import com.photoselector.listener.PhotoItemListener;
import com.photoselector.listener.OnPhotoItemCheckedListener;
import com.photoselector.model.PhotoModel;

/**
 * @author Aizaz AZ
 */
public class PhotoItem extends RelativeLayout implements OnCheckedChangeListener,
        OnLongClickListener, View.OnClickListener {
    private static final String LOG_TAG = PhotoItem.class.getSimpleName();
    private ImageView ivPhoto;
    private CheckBox cbPhoto;
    private OnPhotoItemCheckedListener listener;
    private PhotoModel photo;
    private boolean isCheckAll;
    private PhotoItemClickListener l;
    private int position;
    private PhotoItemListener photoItemListener;


    private PhotoItem(Context context) {
        super(context);
    }

    public PhotoItem(Context context, OnPhotoItemCheckedListener listener) {
        this(context);
        LayoutInflater.from(context).inflate(R.layout.layout_photoitem, this,
                true);
        this.listener = listener;

        setOnLongClickListener(this);

        ivPhoto = (ImageView) findViewById(R.id.iv_photo_lpsi);
        cbPhoto = (CheckBox) findViewById(R.id.cb_photo_lpsi);

        cbPhoto.setOnCheckedChangeListener(this); // // CheckBox选中状态改变监听器
        ivPhoto.setOnClickListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        boolean isReachMax = photoItemListener.reachMax();
        if (!isCheckAll) {
            listener.onCheckedChanged(photo, buttonView, isChecked); // 调用主界面回调函数
        }
        // 让图片变暗或者变亮
        if (isChecked) {
            if (!isReachMax) {
                setDrawingable();
                ivPhoto.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
                photo.setChecked(isChecked);
            }
        } else {
            ivPhoto.clearColorFilter();
            photo.setChecked(isChecked);
        }
    }

    /**
     * 设置路径下的图片对应的缩略图
     */
    public void setImageDrawable(final PhotoModel photo) {
        this.photo = photo;
        DisplayImageOptions imageOptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_picture_loading)
                .showImageOnFail(R.mipmap.ic_picture_loadfailed)
                .cacheInMemory(true)
                .cacheOnDisk(false)
                .resetViewBeforeLoading(true)
                .considerExifParams(false)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        ImageLoader.getInstance().displayImage(
                "file://" + photo.getOriginalPath(), ivPhoto, imageOptions);
    }

    private void setDrawingable() {
        ivPhoto.setDrawingCacheEnabled(true);
        ivPhoto.buildDrawingCache();
    }

    public void setPhotoItemListener(PhotoItemListener listener) {
        this.photoItemListener = listener;
    }

    @Override
    public void setSelected(boolean selected) {
        if (photo == null) {
            return;
        }
        isCheckAll = true;
        cbPhoto.setChecked(selected);
        isCheckAll = false;
    }


    public void setOnClickListener(PhotoItemClickListener l, int position) {
        this.l = l;
        this.position = position;
    }

    @Override
    public void onClick(View v) {
        l.onItemClick(position);
    }


    @Override
    public boolean onLongClick(View v) {
        if (l != null)
            l.onItemClick(position);
        return true;
    }

}
